Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Support date types #36

Closed
wants to merge 2 commits into from

Conversation

LuminaSapphira
Copy link
Collaborator

@LuminaSapphira LuminaSapphira commented Dec 15, 2022

This is the initial pass at supporting DateTime types in Typeshare. The basic gist is to map a chrono::DateTime type to a language-relevant type. Additionally, it will emit serializers for DateTime types if they are used.

Closes #34

Todo

  • Update existing snapshot tests reflecting the changes
  • Add appropriate unit and snapshot tests

Types used

Language Type
Kotlin java.time.Instant
Swift Date (Foundation)
TypeScript Date (JS Global)
Go time.Time

Sample Input Rust Code

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct DateTimeTest {
    pub test: DateTime<Utc>,
    pub uwu: u16,
    pub iwi: i16,
}

Example output

Kotlin

typeshare typeshare-tests/src --lang kotlin --output-file typeshare-tests/typeshare-output/test_def.kt

@Serializable
data class DateTimeTest (
	var test: java.time.Instant,
	var uwu: UShort,
	var iwi: Short
)

// Emits serializer at end of file

import kotlinx.serialization.*

object JavaInstantSerializer : KSerializer<java.time.Instant> {
    override val descriptor = PrimitiveDescriptor("Instant", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: java.time.Instant) = encoder.encodeString(value)
    override fun deserialize(decoder: Decoder): java.time.Instant = java.time.Instant.parse(decoder.decodeString())
}

Swift

/*
 Generated by typeshare 1.0.0
 */

import Foundation

public struct DateTimeTest: Codable {
	public let test: Date /* Foundation's Date is already Codable */
	public let uwu: UInt16
	public let iwi: Int16

	public init(test: Date, uwu: UInt16, iwi: Int16) {
		self.test = test
		self.uwu = uwu
		self.iwi = iwi
	}
}

Typescript

/*
 Generated by typeshare 1.0.0
*/

export interface DateTimeTest {
	test: Date;
	uwu: number;
	iwi: number;
}

// Can be passed to JSON.parse(str, reviver) to marshal the ISO 8601 date string into a Date object
// JSON.stringify will emit a string in the right format by default
export function TypeshareDateReviver(key, value): Date { return new Date(value); }

Go

I have the least experience in Go, and am unsure if extra serializers are needed here.

// Code generated by typeshare 1.0.0. DO NOT EDIT.
package test

import (
    "encoding/json"
    "time"
)

type DateTimeTest struct {
	Test Time `json:"test"`
	Uwu int `json:"uwu"`
	Iwi int `json:"iwi"`
}

@LuminaSapphira LuminaSapphira force-pushed the CerulanLumina/support-date-types branch from f75ddd8 to f20ba35 Compare December 15, 2022 18:40
@LuminaSapphira LuminaSapphira self-assigned this Dec 15, 2022
@LuminaSapphira
Copy link
Collaborator Author

Closing in favor of #85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Special Rust Type: DateTime
2 participants